package com.neuedu.web;

import com.neuedu.pojo.Userinfo;
import com.neuedu.service.UserinfoServiceImpl;
import com.neuedu.utils.MD5Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserLoginServlet",urlPatterns = "/user/login")
public class UserLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        Userinfo userinfo = new Userinfo();
        userinfo.setEmail(email);
        userinfo.setPassword(MD5Utils.getPwd(password)); // 加密
        // 调service
        UserinfoServiceImpl userinfoService = new UserinfoServiceImpl();
        Userinfo userinfo1 = userinfoService.userLogin(userinfo);
        if (userinfo1 == null){
            response.getWriter().println(0);
        }else{
            // 登录成功
            request.getSession().setAttribute("login_user",userinfo1);
            response.getWriter().println(1);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
